<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="stop">停止</div>
    <script>
        function mySetInterval(fn, wait) {
            let timer = null
            function loop() {
                timer = setTimeout(() => {
                    loop()
                    fn()
                }, wait)
                console.log(timer)
            }
            loop()
            // 返回停止的方法
            return () => {
                clearTimeout(timer)
            }
        }

        let stop = mySetInterval(() => {
            console.log(11)
        }, 1000)

        let box = document.getElementById('stop')
        box.addEventListener('click', () => {
            stop()
        })
    </script>
</body>

</html>